package com.demo.utils;

import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 测试post和get请求示例
 */
public class TestCase {

    /**
     * 网关前缀地址，注意替换正式网关地址：
     */
    public static final String API_URL = "https://api.smartedu.cn";

    /**
     * 应用ID， appId
     */
    public static final String APP_ID = "WxTKkkNCSi0PlCukgUAVETLnm0qpkAQZ";

    /**
     * 应用KEY，appKey
     */
    public static final String APP_KEY = "pROGluPyEGTItzDRQeg9BLHXNzNmUSQu";

    /**
     * 测试POST请求示例： ，4.3.1.3	根据访问令牌获取用户通行证信息
     * @return
     * @throws Exception
     */
    public static String postCase() throws Exception {

        //根据access_token获取用户信息，请求具体后缀地址
        String url = "/data/user/getUserInfo";

        //Timestamp 时间戳
        long ms = System.currentTimeMillis();
        String Timestamp = String.valueOf(ms);
        //Nonce， 随机字符串
        String Nonce = UUID.randomUUID().toString();

        //Signature
        Map<String, String> headers = new HashMap<>();

        //Jsondata，请求body
        String json="{\"access_token\":\"1f40fec8-848f-4e38-8b90-6fcac831b731\"}";

        headers.put("Cc-Appid",APP_ID);
        headers.put("Cc-Timestamp",Timestamp);
        headers.put("Cc-Nonce",Nonce);
        String Signature = SignUtil.serviceSign(url,"POST",
                headers,new HashMap<>(),json, APP_KEY);

        HttpClient httpClient = HttpClients.createDefault();
        HttpPost request = new HttpPost(API_URL + url);
        StringEntity entity = new StringEntity(json, ContentType.create("application/json", Consts.UTF_8));
        request.setEntity(entity);
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Cc-Appid", APP_ID);
        request.setHeader("Cc-Timestamp", Timestamp);
        request.setHeader("Cc-Nonce", Nonce);
        request.setHeader("Cc-Signature", Signature);

        HttpResponse response = httpClient.execute(request);
        String result = null;
        if(response != null){
            HttpEntity resEntity = response.getEntity();
            if(resEntity != null){
                result = EntityUtils.toString(resEntity, "utf-8");
            }
        }
        return result;
    }

    /**
     * 测试GET请求示例，仅供参考, 4.3.6.1	获取区域下行政区划列表
     * @return
     * @throws Exception
     */
    public static String getCase() throws Exception {
        String url="/stat/v1/areas?area_code=0";
        //Timestamp
        long ms = System.currentTimeMillis();
        String Timestamp = String.valueOf(ms);
        //Nonce
        String Nonce = UUID.randomUUID().toString();

        //Signature
        Map<String, String> headers = new HashMap<>();
        headers.put("Cc-Appid",APP_ID);
        headers.put("Cc-Timestamp",Timestamp);
        headers.put("Cc-Nonce",Nonce);
        Map<String, String> queryMap = new HashMap<>();
        queryMap.put("area_code","0");
        String Signature = SignUtil.serviceSign("/stat/v1/areas","GET",headers,
                queryMap,null,APP_KEY);

        HttpClient httpClient = HttpClients.createDefault();
        HttpGet request = new HttpGet(API_URL + url);
        request.setHeader("Content-Type", "application/json");
        request.setHeader("Cc-Appid", APP_ID);
        request.setHeader("Cc-Timestamp", Timestamp);
        request.setHeader("Cc-Nonce", Nonce);
        request.setHeader("Cc-Signature", Signature);
        HttpResponse response = httpClient.execute(request);
        String result = null;
        if(response != null){
            HttpEntity resEntity = response.getEntity();
            if(resEntity != null){
                result = EntityUtils.toString(resEntity, "utf-8");
            }
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        //String result = TestCase.getCase();
        String result = TestCase.postCase();
        System.out.println(result);
    }
}
